﻿
Public NotInheritable Class CTBSO 



Private _BSOId              As Long
Private _SecBSO             As Long
Private _EstatusBSOID       As enumEstatusBSO
Private _FecOperacion       As Date
Private _FecCreacion        As Date  
Private _UsuGestor          As integer
Private _LstPS              As List(Of CTPartidaSolicitud)
Private _lstPAJ             As List(Of CTPartidaAjuste)
Private _lstPEJ             As List(Of CTPartidaEJ)

Private _CatalogoEstatusID As Integer = 3


Private servicio As CSServiciosGenerales = New CSServiciosGenerales()


Public Enum enumEstatusBSO
 NO_CREADA =  0  'Estatus inicial 
 ABIERTA   =  1
 CERRADA   =  2
 CANCELADA =  3
End Enum

Public Enum enumTipoAjuste
 PARA_PS  = 1
 PARA_PEJ = 2
End Enum





#Region "Patrón Singleton"

    Private Shared _Instance As CTBSO = Nothing
    Private Shared ReadOnly _Sync As New Object

        Private Sub New()
            servicio = New CSServiciosGenerales()
            _EstatusBSOID = enumEstatusBSO.NO_CREADA
            _FecOperacion = servicio.ObtieneFechaParamBSO()
            _LstPS = New List(Of CTPartidaSolicitud)
            _lstPAJ = New List(Of CTPartidaAjuste)
            _lstPEJ = New List(Of CTPartidaEJ)
        End Sub

        Public Shared ReadOnly Property Instance() As CTBSO
            Get
                If _Instance Is Nothing Then
                    SyncLock _Sync
                        If _Instance Is Nothing Then
                            _Instance = New CTBSO()
                        End If
                    End SyncLock
                End If
                Return _Instance
            End Get
        End Property

#End Region



Public Property BSOId() As Long
    Get
        Return _BSOId
    End Get
    Set(ByVal value As Long)
        _BSOId = value
    End Set
End Property

Public Property SecBSO() As Long
    Get
        Return _SecBSO
    End Get
    Set(ByVal value As Long)
        _SecBSO = value
    End Set

End Property

Public Property FecOperacion() As Date
    Get
        Return _FecOperacion
    End Get
    Set(ByVal value As Date)
        _FecOperacion = value
    End Set
End Property

Public Property FecCreacion() As Date
    Get
        Return _FecCreacion
    End Get
    Set(ByVal value As Date)
        _FecCreacion = value
    End Set

End Property

Public Property UsuGestor() As Integer
    Get
        Return _UsuGestor
    End Get
    Set(ByVal value As Integer)
        _UsuGestor = value
    End Set
End Property

Public Property EstatusBSOID() As enumEstatusBSO
    Get
        Return _EstatusBSOID 
    End Get
    Set(ByVal value As enumEstatusBSO)
        _EstatusBSOID = value
    End Set
End Property

Public Property LstPS() As List(Of CTPartidaSolicitud)
    Get
        Return _LstPS
    End Get
    Set(ByVal value As List(Of CTPartidaSolicitud))
        _LstPS = value
    End Set
End Property

Public Property LstPAJ() As List(Of CTPartidaAjuste)
    Get
        Return _lstPAJ
    End Get
    Set(ByVal value As List(Of CTPartidaAjuste))
        _lstPAJ = value
    End Set
End Property


Public Property LstPEJ() As List(Of  CTPartidaEJ)
    Get
        Return _lstPEJ
    End Get
    Set(ByVal value As List(Of CTPartidaEJ))
        _lstPEJ = value
    End Set
End Property

Public ReadOnly Property CatalogoEstatusID() As Integer

    Get
        Return _CatalogoEstatusID
    End Get

End Property

#Region "Servicios de la BSO"

   Public Function ExisteBSOActiva As Boolean
      return true
   End Function

   Public Function CrearBSO(ByRef eventoBit As CSBitEvento) As Boolean
        Try
            Return servicio.CrearBSO(eventoBit)
        Catch ex As Exception
            Throw ex
        End Try

    End Function

    Public Function CargarBSOActual() As Boolean
        Try
            Return servicio.CargarBSOActual()
        Catch ex As Exception
            Throw ex
        End Try

    End Function

   Public Function CerrarBSO(ByVal fechaApertura As Date, ByRef eventoBit As CSBitEvento) As Boolean
        Try
            eventoBit.GuadarDetalle("idBSO", BSO.BSOId)
            Return servicio.CerrarBSO(fechaApertura, eventoBit)
        Catch ex As Exception
            Throw ex
        End Try
   End Function

   Public Function CancelarBSO() As Boolean
      return true
   End Function
   
#End Region



#Region "Administración de las PS"

     

  #Region "Administración de acciones sobre las PS"

     Public Function AgregarPS(ByRef pPs As CTPartidaSolicitud, ByRef eventoBit As CSBitEvento) As Boolean        
            Try
                eventoBit.GuadarDetalle("PSObj", pPs.ToString())
                Return servicio.CrearPS(pPs)
            Catch ex As Exception
                Dim errorBit = eventoBit.GuardarError("Error al crear PS: " & ex.Message)
                Throw New Exception(ex.Message & " -Codigo:" & errorBit.IdError)
            End Try                   
      End Function

      Public Function ModificarPS(ByRef pPs As CTPartidaSolicitud) As Boolean
            Try
                Return servicio.ModificarPS(pPs)
            Catch ex As Exception
                Throw ex
            End Try     
      End Function

      Public Function EliminarPS(ByVal pPSId As Long) As Boolean

         Return True 
      End Function

      Public Function CerrarPS(ByRef pPs As CTPartidaSolicitud, ByVal comentario As String) As Boolean
            Try
                Return servicio.CerrarPS(pPs, comentario)
            Catch ex As Exception
                Throw ex
            End Try     
      End Function

      Public Function CerrarIncompletaPS(ByRef pPs As CTPartidaSolicitud, ByVal comentario As String, ByRef eventoBit As CSBitEvento) As Boolean
            Try
                Return servicio.CerrarIncompletaPS(pPs, comentario, eventoBit)
            Catch ex As Exception
                Throw ex
            End Try     
      End Function
      Public Function FusionarLotePS(ByRef pLtsPSId As List(Of Long) ) As Boolean
         Return true
      End Function

   #End Region

  #Region "Administración del estatus"
  

          Public Function EnviarLotePS(ByRef pLstPSId As List(Of Long)) As Boolean
     
              'Notificación masiva      
               Return True

           End Function

          Public Function EnviarPS(ByRef pPSId As Long) As Boolean

                Try
                    Return servicio.EnviarPS(pPSId)
                Catch ex As Exception
                    Throw ex
                End Try  
          End Function         

          Public Function PasarPSEnviadoaEnProceso(ByRef pPSId As Long) As Boolean 

               Return true
          End Function

          Public Function PasarPSEnProcesoALiberada(ByVal pPSId As Long) As Boolean

                'Me.LstPS.Find (Function(x) x.PartidaSolId = pPSId)

            'Se libera para distribución la partida 
             Return true
          End Function




          Public Function PasarLotePSEnProcesoALiberada(Byref pLstPSId As List(Of Long)) As Boolean
            Return True 
          End Function

          Public Function PasarPSLiberadaAEnDistribucion(ByVal pPSId As long) As Boolean
             'Puede ser invocada desde la distribución GL
             Return true
          End Function

          Public Function PasarLotePSLiberadaAEnDistribucion(ByVal pLtsPSId As List(Of long)) As Boolean
             'Puede ser invocada desde la distribución GL
             Return true
          End Function

          Public Function PasarPSEnDistribucionACerrada(ByVal pPSId As long) As Boolean
             'Puede ser invocada desde la distribución GL
             Return true
          End Function

          Public Function PasarLotePSEnDistribucionACerrada(ByVal pLtsPSId As List(Of long)) As Boolean
            'Posible incovacion desde la distribución 

            Return True 
          End Function



          Public Function ReversarPSDeLiberadaEAEnProceso(ByVal pPsId As Long) As Boolean
                'Para efectos de corrección 
                Return True 
          End Function

          Public Function ReversarPSdeEnDistribucionAEnProceso(ByVal pPsId As Long) As Boolean
                'Para efectos de corrección 'Solo se pueden modificar las PEJ que no han sido distribuidas
                Return True 
          End Function
                         



          Public Function CancelarPS(ByVal idPartida As Long) As Boolean
            Try
                Return servicio.CancelarPS(idPartida)
            Catch ex As Exception
                Throw ex
            End Try  
          End Function


  #End Region


#End Region




#Region "Administración de las partidad de Ajuste"

  Public Function AgregarPAJ(ByRef pPAJ As CTPartidaAjuste) As Boolean
        Try
            Return servicio.CrearPAJ(pPAJ)
        Catch ex As Exception
            Throw ex
        End Try   
  End Function

  Public Function ModificarPAJ(ByRef pPAJ As CTPartidaAjuste) As Boolean
        Try
            Return servicio.ModificarPAJ(pPAJ)
        Catch ex As Exception
            Throw ex
        End Try   
  End Function

  Public Function EnviarPAJ(ByRef pPAJID As Long) As Boolean
    Try
        Return servicio.EnviarPA(pPAJID)
    Catch ex As Exception
        Throw ex
    End Try 
  End Function

  Public Function AplicarLotePAJ(ByRef pLstPAJID As List(Of long)) As Boolean
    'EJ


    Return True
  End Function

  Public Function CancelarPAJ(ByVal idPartidaAjuste As Long, ByRef estatus As String) As Boolean
    Try
        Return servicio.CancelarPA(idPartidaAjuste, estatus)
    Catch ex As Exception
        Throw ex
    End Try 
  End Function


#End Region








End Class
